package com.lsq.tucao.express.service.serviceImpl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsq.tucao.express.bean.po.HeartDetail;
import com.lsq.tucao.express.dao.HeartDetailMapper;
import com.lsq.tucao.express.response.JsonResult;
import com.lsq.tucao.express.service.HeartExpressService;
import com.lsq.tucao.express.utils.SensitiveWordFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class HeartExpressServiceImpl implements HeartExpressService {

    @Autowired
    private HeartDetailMapper heartDetailMapper;
    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    @Override
    public JsonResult addHeart(HeartDetail heartDetail) {
        if(heartDetail != null && !StringUtils.isEmpty(heartDetail.getContent())){
            //检测发布的吐槽中是否包含敏感词汇
            Set<String> set = sensitiveWordFilter.getSensitiveWord(heartDetail.getContent(), 1);
            if(set.size() > 0){
                return new JsonResult(500,"发布的鸡汤中含有违禁词"+set);
            }else {
                int insert = heartDetailMapper.insert(heartDetail);
                if(insert > 0){
                    return new JsonResult(200,"发布成功");
                }else {
                    return new JsonResult(500,"发布失败");
                }
            }

        }else {
            return new JsonResult(500,"请输入要发布的鸡汤内容");
        }
    }

//    @Override
//    public JsonResult likeHeart(Long heartId) {
//        if(heartId != null){
//            Integer update = heartDetailMapper.likeHeart(heartId);
//            if(update > 0){
//                return new JsonResult(200,"点赞成功");
//            }else {
//                return new JsonResult(500,"点赞失败");
//            }
//        }else {
//            return new JsonResult(500,"请传入要点赞的鸡汤Id");
//        }
//    }

    @Override
    public JsonResult pageHeart(Integer current, Integer size, Integer verify, Boolean getVerified) {
        Page<HeartDetail> page = new Page<>(current,size);
        QueryWrapper<HeartDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HeartDetail::getIsDelete,1)
                .orderByDesc(HeartDetail::getCreateTime);

        if(verify != null){
            queryWrapper.lambda().eq(HeartDetail::getVerify,verify);
        }
        if(getVerified){
            queryWrapper.lambda().ge(HeartDetail::getVerify,1);
        }

        Page<HeartDetail> heartDetailPage = heartDetailMapper.selectPage(page, queryWrapper);
        if(heartDetailPage != null){
            return new JsonResult(heartDetailPage,200,"分页查询数据成功");
        }else {
            return new JsonResult(500,"分页查询数据失败");
        }
    }

    @Override
    public JsonResult getHeartById(Long heartId) {
        if(heartId != null){
            HeartDetail heartDetail = heartDetailMapper.selectById(heartId);
            if(heartDetail != null && heartDetail.getIsDelete() == 1){
                return new JsonResult(heartDetail,200,"获取数据成功");
            }else {
                return new JsonResult(500,"获取数据失败");
            }
        }else {
            return new JsonResult(500,"请传入参数[heartId]");
        }
    }

    @Override
    public JsonResult verifyHeart(Long heartId, Boolean verify) {
        if(heartId != null){
            if(verify != null){
                UpdateWrapper<HeartDetail> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(HeartDetail::getId,heartId);
                //审核通过：1；审核未通过：2
                if(verify){
                    updateWrapper.lambda().set(HeartDetail::getVerify,1);
                }else {
                    updateWrapper.lambda().set(HeartDetail::getVerify,2);
                }
                int update = heartDetailMapper.update(null, updateWrapper);
                if(update > 0){
                    return new JsonResult(200,"审核成功");
                }else {
                    return new JsonResult(500,"审核失败");
                }
            }else {
                return new JsonResult(500,"请传入审核结果（verify）");
            }
        }else {
            return new JsonResult(500,"请传入有效的heartId");
        }
    }

    @Override
    public JsonResult searchHeartByContent(Integer current, Integer size, String heartContent) {
        if(!StringUtils.isEmpty(heartContent)){
            Page<HeartDetail> heartPage = new Page<>(current,size);
            QueryWrapper<HeartDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(HeartDetail::getIsDelete,1)
                    .eq(HeartDetail::getVerify,1)
                    .like(HeartDetail::getContent,heartContent);

            Page<HeartDetail> heartDetailPage = heartDetailMapper.selectPage(heartPage,queryWrapper);
            if (heartDetailPage != null && heartDetailPage.getRecords().size() > 0) {
                return new JsonResult(heartDetailPage,200,"搜索鸡汤成功");
            }else {
                return new JsonResult(500,"搜索鸡汤失败");
            }
        }else {
            return new JsonResult(500,"请输入要查询的内容");
        }
    }

    @Override
    public JsonResult searchHeartByUsername(Integer current, Integer size, String username) {
        if(!StringUtils.isEmpty(username)){
            Page<HeartDetail> heartPage = new Page<>(current,size);
            QueryWrapper<HeartDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(HeartDetail::getIsDelete,1)
                    .eq(HeartDetail::getVerify,1)
                    .like(HeartDetail::getUsername,username);

            Page<HeartDetail> heartDetailPage = heartDetailMapper.selectPage(heartPage,queryWrapper);
            if (heartDetailPage != null && heartDetailPage.getRecords().size() > 0) {
                return new JsonResult(heartDetailPage,200,"搜索鸡汤成功");
            }else {
                return new JsonResult(500,"搜索鸡汤失败");
            }
        }else {
            return new JsonResult(500,"请输入要查询的用户名");
        }
    }

    @Override
    public JsonResult sortHeartBy(Integer current, Integer size, String sortBy) {
        if(!StringUtils.isEmpty(sortBy)){
            Page<HeartDetail> heartPage = new Page<>(current,size);
            QueryWrapper<HeartDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(HeartDetail::getIsDelete,1)
                    .eq(HeartDetail::getVerify,1);

            if(sortBy.equals("按发布时间升序")){
                queryWrapper.lambda().orderByAsc(HeartDetail::getCreateTime);
            }else if(sortBy.equals("按发布时间降序")){
                queryWrapper.lambda().orderByDesc(HeartDetail::getCreateTime);
            }else if(sortBy.equals("按点赞数升序")){
                queryWrapper.lambda().orderByAsc(HeartDetail::getLikeNum);
            }else if(sortBy.equals("按点赞数降序")){
                queryWrapper.lambda().orderByDesc(HeartDetail::getLikeNum);
            }else if(sortBy.equals("按评论数升序")){
                queryWrapper.lambda().orderByAsc(HeartDetail::getDiscussNum);
            }else if(sortBy.equals("按评论数降序")){
                queryWrapper.lambda().orderByDesc(HeartDetail::getDiscussNum);
            }else {
                return new JsonResult(500,"请传入有效的排序条件");
            }

            Page<HeartDetail> heartDetailPage = heartDetailMapper.selectPage(heartPage,queryWrapper);
            if (heartDetailPage != null)
            {
                return new JsonResult(heartDetailPage,200,"鸡汤排序成功");
            }else {
                return new JsonResult(500,"鸡汤排序失败");
            }

        }else {
            return new JsonResult(500,"请传入排序条件");
        }
    }

}
